/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.
 */
package td.domain.util;

import java.util.ArrayList;

import td.domain.DatastoreObject;

/**
 * Cache of DataStore Ids.
 * 
 * @author Artem Shamsutdinov
 * 
 */
public class IdCache extends DatastoreObject {

	private static final long serialVersionUID = -446159251102455085L;

	// Tis an ARRAYList because of GWT (less code to transfer)
	private ArrayList<Long> ids = new ArrayList<Long>();

	private boolean hasRolledOffValues = false;

	/**
	 * Adds an entry this Id cache with most recent element being the last one.
	 * This inverted indexing strategy speeds up the insertion of records but
	 * adds logic when looking up IDs.
	 * 
	 * @param newId
	 *            The Id to insert
	 * @return The Id that rolled off the cache (if any)
	 */
	public Long add(Long newId) {
		Long rolledOffId = null;
		if (ids.size() >= DatastoreObject.ID_CACHE_MAX_LENGTH) {
			rolledOffId = ids.remove(0);
			hasRolledOffValues = true;
		}
		ids.add(newId);

		return rolledOffId;
	}

	/**
	 * Remove an entry from this cache.
	 * 
	 * @param newId
	 *            The Id to remove
	 * @return The Id that was removed from the cache (if any)
	 */
	public Long remove(Long idToRemove) {
		for (int i = 0; i < ids.size(); i++) {
			if (ids.get(i).equals(idToRemove)) {
				return ids.remove(i);
			}
		}
		return null;
	}

	/**
	 * Gets a specified page out of this Id Cache.
	 * 
	 * @param virtualStartOffset
	 *            as specified by the GUI (vs. real offset being backwards in
	 *            the cache).
	 * @param pageSize
	 *            Number of records to return
	 * @return Requested records
	 */
	public ArrayList<Long> getPage(int virtualStartOffset, int pageSize) {
		ArrayList<Long> resultSet = new ArrayList<Long>();

		// The -1 is if virtualStartOffset begins with 0 (if it begins with 1
		// then
		// -1 should be removed)
		int realOffset = ids.size() - virtualStartOffset - 1;

		for (int i = realOffset, j = 0; i > realOffset - pageSize; i--, j++) {
			if (i < 0) {
				break;
			}
			resultSet.add(j, ids.get(i));
		}

		return resultSet;
	}

	/**
	 * Get's the last value of the cache.
	 * 
	 * @return
	 */
	public Long getNewest() {
		if (ids.size() == 0) {
			return null;
		}
		return ids.get(ids.size() - 1);
	}

	public int size() {
		return ids.size();
	}

	public Long getAtIndex(int index) {
		return ids.get(index);
	}

	public boolean getHasRolledOffValues() {
		return hasRolledOffValues;
	}

}
